<template>
  <div>
    <el-button type="text" @click="dialogFormVisible = true; operateType = 'save'">添加品牌</el-button>

    <el-dialog title="搜索历史信息" :visible.sync="dialogFormVisible" @close="handleClose">
      <el-form :model="search" accept-charset="UTF-8">
        <el-form-item label="搜索编号" :label-width="formLabelWidth">
          <el-input type="text" v-model="search.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="搜索词" :label-width="formLabelWidth">
          <el-input v-model="search.search_words" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="搜索次数" :label-width="formLabelWidth">
          <el-input v-model="search.num" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="搜索时间" :label-width="formLabelWidth">
          <el-input v-model="search.search_time" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlesave">确 定</el-button>
      </div>
    </el-dialog>

    <!--数据表格-->
    <el-table :data="searches" style="width: 100%">
      <el-table-column label="搜索历史编号" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="搜索词" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.search_words }}</span>
        </template>
      </el-table-column>
      <el-table-column label="搜索次数" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="搜索时间" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.search_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination background layout="prev, pager, next" :page-size="pageSize" :total="total" :current-page="current"
      @current-change="loadSearchs">
    </el-pagination>

  </div>
</template>


<script>
export default {
  name: 'SearchHistory',
  data() {
    return {
      searches: [],  //品牌数组
      dialogFormVisible: false, //控制对话框隐藏显示
      formLabelWidth: '120px',
      search: { id: '', search_words: '', num: '', search_time: '' }, //品牌数据
      operateType: '', //操作类型   save update
      total: 0,     //总页数
      pageSize: 5,  //每一页行数
      current: 0   //当前页数
    }
  },
  methods: {
    //加载品牌列表的方法
    loadSearchs(current) {
      this.current = current
      this.$http.get('/search?m=page&current=' + this.current + "&pageSize=" + this.pageSize).then(resp => {
        console.log(resp.data)
        if (resp.data.data) {
          this.searches = resp.data.data.records
          this.total = resp.data.data.total
        }
      })
    },
    handleEdit(index, row) {
      console.log(index, row);
      this.operateType = 'update';
      //显示对话框，绑定数据
      this.dialogFormVisible = true;
      //进行深克隆 row---->string---->json
      this.search = JSON.parse(JSON.stringify(row));
      // this.brand.brand_img ='';
    },
    handlesave() {
      // let url = encodeURI(this.$qs.stringify(this.brand))
      console.log('save search', this.search, this.$qs.stringify(this.search))
      if (this.operateType == 'save') {
        //添加品牌
        this.$http.post('/search?m=save', this.$qs.stringify(this.search))
          .then(resp => {
            console.log('save brand', resp.data)
            if (resp.data.data == true) {
              //添加成功，刷新品牌列表
              console.log("sucessfully")
              this.loadSearchs(this.current);
            }
          });
      } else {
        //更新用户
        this.$http.post('/search?m=update', this.$qs.stringify(this.search))
          .then(resp => {
            console.log('update search', resp.data)
            if (resp.data.data == true) {
              //添加成功，刷新用户列表
              this.loadSearchs(this.current);
            }
          })
      }
      this.dialogFormVisible = false

    },
    handleDelete(index, row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.post('/search?m=remove&id=' + row.id).then(resp => {
          if (resp.data.data == true) {
            this.loadSearchs(this.current);
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      console.log(index, row);
    },
    handleClose() {
      this.search = {
        id: '',
        search_words: '',
        num: '',
        search_time: ''
      }
      this.operateType = ''
    }
  },
  mounted() {
    this.loadSearchs(1)
  }


}
</script>

<style scoped></style>